<?php

namespace backend\controllers;

use Yii;
use backend\models\form\ColumnForm;
use backend\service\ColumnService;
use backend\service\RbacService;
use yii\helpers\Url;
use backend\models\form\RoleForm;
use backend\models\form\RolePermissionForm;
use backend\models\form\UserAssignmentForm;
use backend\models\form\PermissionForm;
use backend\models\form\UploadForm;
use yii\web\UploadedFile;

/**
 * 栏目管理类
 */
class ColumnController extends BaseController {

    /**
     * 顶级模块页面
     */
    public function actionIndex() {
        return $this->render("index");
    }

    /**
     * 增加栏目
     */
    public function actionAddColumn() {
        $model = new ColumnForm();
        $post_data = Yii::$app->request->post();
        if ($model->load($post_data) && $model->validate()) {
            $add_rs = $model->addColumn();
            if ($add_rs['status'] == true) {
                RbacService::addPermissionbyColumn($model->pid, $model->tag);
            }
            return $this->redirect(["tip/index", "msg" => $add_rs['msg'], "url" => Url::toRoute(["/column/index"])]);
        }
        return $this->render("add_column", ["model" => $model]);
    }

    /**
     * 子栏目页面
     */
    public function actionSubColumn() {
        return $this->render("sub_column");
    }

    /**
     * 更新栏目
     */
    public function actionUpdColumn() {
        $column_id = Yii::$app->request->get("column_id");
        if (empty($column_id)) {
            return $this->redirect(["tip/index", "msg" => "请不要乱操作", "url" => Url::toRoute(["/column/index"])]);
        }
        $column = ColumnService::findColumnById($column_id);
        $model = new ColumnForm();
        $post_data = Yii::$app->request->post();
        if ($model->load($post_data) && $model->validate()) {
            $add_rs = $model->addColumn();
            if ($add_rs['status'] == true) {
                RbacService::addPermissionbyColumn($model->pid, $model->tag, $column['tag']);
            }
            return $this->redirect(["tip/index", "msg" => $add_rs['msg'], "url" => Url::toRoute(["/column/sub-column"])]);
        }
        $model->setAttributes($column->getAttributes());
        return $this->render("upd_column", ["model" => $model]);
    }

    /**
     * 删除栏目
     */
    public function actionDelColumn() {
        $column_id = Yii::$app->request->get("column_id");
        if (empty($column_id)) {
            return $this->redirect(["tip/index", "msg" => "请不要乱操作", "url" => Url::toRoute(["/column/index"])]);
        }
        $column = ColumnService::findColumnById($column_id);
        $del_rs = ColumnService::delColumnById($column_id);
        if ($del_rs['status'] == true) {
            RbacService::removePermissionbyColumn($column);
        }
        return $this->redirect(["tip/index", "msg" => $del_rs['msg'], "url" => Yii::$app->getRequest()->referrer]);
    }

    public function actionPermission() {
        $role_name = Yii::$app->request->get("role_name");
        if (!empty($role_name)) {
            $role_name = Yii::$app->request->get("role_name");
            $permissions = RbacService::findPermissionsByRole($role_name);
        } else {
            $permissions = RbacService::findPermissions();
        }
        return $this->render("permission", ["permissions" => $permissions]);
    }

    public function actionAddRolePermission() {
        $role_name = Yii::$app->request->get("role_name");
        if (empty($role_name)) {
            return $this->redirect(["tip/index", "msg" => "请不要乱操作", "url" => Url::toRoute(["/column/role"])]);
        }
        $model = new RolePermissionForm();
        $post_data = Yii::$app->request->post();
        if ($model->load($post_data) && $model->validate()) {
            $add_rs = $model->addRolePermission();
            return $this->redirect(["tip/index", "msg" => $add_rs['msg'], "url" => Url::toRoute(["/column/role"])]);
        }
        return $this->render("add_role_permission", ["model" => $model]);
    }


    /**
     * 增加权限
     */
    public function actionAddPermission() {
        $model = new PermissionForm();
        $post_data = Yii::$app->request->post();
        if ($model->load($post_data) && $model->validate()) {
            $add_rs = $model->addPermission();
            return $this->redirect(["tip/index", "msg" => $add_rs['msg'], "url" => Url::toRoute(["/column/permission"])]);
        }
        return $this->render("add_permission", ["model" => $model]);
    }

    /**
     * 上传
     */
    public function actionUpload() {
        $model = new UploadForm();
        if (Yii::$app->request->isPost) {
            $model->file = UploadedFile::getInstance($model, 'file');
            if ($model->file && $model->validate()) {
                $image_url = Yii::getAlias("@image/web");
                $model->file->saveAs($image_url . '/uploads/' . $model->file->baseName . '.' . $model->file->extension);
            }
        }

        return $this->render('upload', ['model' => $model]);
    }

    /**
     * 删除权限
     */
    public function actionDelPermission() {
        $role_name = Yii::$app->request->get("role_name");
        if (empty($role_name)) {
            return $this->redirect(["tip/index", "msg" => "请不要乱操作", "url" => Url::toRoute(["/column/permission"])]);
        }
        $del_rs = RbacService::delPermission($role_name);
        return $this->redirect(["tip/index", "msg" => $del_rs['msg'], "url" => Url::toRoute(["/column/permission"])]);
    }

}
